package com.jd.pet.service.impl;

import com.jd.basic.exception.BusinessException;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.BaiduAIUtils;
import com.jd.basic.util.PageList;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.PetDetail;
import com.jd.pet.domain.PetOnlineAuditLog;
import com.jd.pet.mapper.PetDetailMapper;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.PetOnlineAuditLogMapper;
import com.jd.pet.query.PetQuery;
import com.jd.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * crud宠物需要对宠物类型表进行维护
     * @param pet
     */
    @Override
    @Transactional
    public void add(Pet pet) {
        pet.setState(0);
        super.add(pet); // 宠物添加
        // 添加宠物详情
        pet.getPetDetail().setPetId(pet.getId());
        petDetailMapper.add(pet.getPetDetail());
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail detail = petDetailMapper.findByPetId(pet.getId());
        detail.setIntro(pet.getPetDetail().getIntro());
        detail.setAdoptNotice(pet.getPetDetail().getAdoptNotice());
        petDetailMapper.update(detail);
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        // 删除对应宠物详情表
        petDetailMapper.delByPetId(id);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        super.patchDel(ids);
        // 删除对应宠物详情表
        petDetailMapper.patchDelByPetId(ids);
    }

    @Override
    public AjaxResult listing(Long[] ids) {
        // 上架宠物，修改状态为上架
        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            if (pet.getState() == 1){
                throw new BusinessException("已上架，请勿重复上架！");
            }
            // 对要上架的宠物进行智能审核
            if (!BaiduAIUtils.TextCensor(pet.getName())){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(0);
                log.setAuditTime(new Date(System.currentTimeMillis()));
                log.setNote("审核失败！名称违规！");
                petOnlineAuditLogMapper.add(log);
                throw new BusinessException("宠物名称违规！");
            }
            if (BaiduAIUtils.ImgsCensor(pet.getResources())){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(0);
                log.setAuditTime(new Date(System.currentTimeMillis()));
                log.setNote("审核失败！图片违规！");
                petOnlineAuditLogMapper.add(log);
                throw new BusinessException("宠物图片违规！");
            }
            // 审核成功添加审核日志
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(pet.getId());
            log.setState(1);
            log.setAuditTime(new Date(System.currentTimeMillis()));
            log.setNote("审核成功！");
            petOnlineAuditLogMapper.add(log);
            pet.setOnsaletime(new Date(System.currentTimeMillis()));
            petMapper.update(pet);
            }
        petMapper.listingById(ids);
        return new AjaxResult();
    }

    @Override
    public AjaxResult delist(Long[] ids) {
        // 下架宠物，修改状态为下架
        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            if (pet.getState() == 0){
                throw new BusinessException("已下架，请勿重复下架！");
            } else {
                pet.setOffsaletime(new Date(System.currentTimeMillis())); // 设置下架时间
                petMapper.update(pet);
            }
        }
        petMapper.delistById(ids);
        return new AjaxResult();
    }

    @Override
    public PageList<Pet> queryPageFormat(PetQuery query) {
            // 查询总条数
            Long count = petMapper.queryCountFormat(query);
            // 判断如果没有就直接返回空集合
            if (count == 0){
                return new PageList<>();
            }
            // 查询当前页数据
            List<Pet> list = petMapper.queryDataFormat(query);
            // 分页数据封装成PageList
            return new PageList<>(count,list);
    }
}
